//
//  ITListController.m
//  InTubePro
//
//  Created by seven on 2020/2/7.
//  Copyright © 2020 seven. All rights reserved.
//

#import "ITListController.h"
@interface ITListController ()

@end

@implementation ITListController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (_member == nil) {
        _member = ITUserManager.currentMember;
    }
    
    if (_dataSource == nil) {
        self.dataSource = NSMutableArray.array;
    }

    //self.view.backgroundColor = ColorWithHex(0xf1f1f1);
    _tableView = [BaseTableView initWithFrame:self.contentFrame];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = COLOR_CLEAR;
    _tableView.showsVerticalScrollIndicator = NO;
    //[_tableView registerClass:ITFollowCell.class forCellReuseIdentifier:@"cell"];
    _tableView.rowHeight = 74;
    _tableView.estimatedRowHeight = _tableView.rowHeight;
    _tableView.estimatedSectionFooterHeight = 0;
    _tableView.estimatedSectionHeaderHeight = 0;
    //_tableView.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [self.view addSubview:self.topView];
    adjustsScrollViewInsets(_tableView);
    WEAKSELF
    _tableView.mj_header = [ITRefreshGifHeader headerWithRefreshingBlock:^{
        STRONGSELF
        [weak_self refreshTableViewData];
    }];
    _tableView.mj_footer = [ITRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    _page = 1;
    // Do any additional setup after loading the view.
    //[self loadData];
    [self autoRefreshWhenDidLoad];
}

- (void)autoRefreshWhenDidLoad{
    [_tableView.mj_header beginRefreshing];
}

- (void)updateContentFrame{
    [super updateContentFrame];
    _tableView.frame = self.contentFrame;
}

- (void)refreshTableViewData{
    self.timestamp = 0;
    self.page = 1;
    _skip = 0;
    _noMoreData = NO;
    [self loadData];
}

- (void)onEmptyAction{
    [self.view showLoading];
    [self loadData];
}

- (void)loadData{

}

- (void)didLoadWithData:(NSArray *)data{
    NSMutableArray *tmp = data;
    /*if (tmp.count == 0) {
        tmp = self.randomList;
    }//*/
    if (_page == 1) {
        if (self.ignoreEmptyView) {
            _tableView.mj_footer.alpha = 1;
        }else {
            if (tmp == nil && _dataSource.count==0){
                [self showEmptyView:0];
            }else {
                [self hideEmptyView];
            }
        }
    }
    if (tmp == nil) {
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
        return;
    }
    
    if (_page == 1) {
        [_dataSource removeAllObjects];
//        [_dataSource setArray:tmp];
//        [_tableView reloadData];
    }else {
//        NSMutableArray *paths = NSMutableArray.array;
//        for (int i=0; i<tmp.count; i++) {
//            [paths addObject:[NSIndexPath indexPathForRow:_dataSource.count+i inSection:0]];
//        }
//        [_tableView beginUpdates];
//        [_dataSource addObjectsFromArray:tmp];
//        [_tableView insertRowsAtIndexPaths:paths withRowAnimation:(UITableViewRowAnimationFade)];
//        [_tableView endUpdates];
    }
    [_dataSource addObjectsFromArray:tmp];
    [_tableView reloadData];
    
    if (_page == 1) {
        [_tableView.mj_header endRefreshing];
    }
    _tableView.mj_footer.alpha = 1;
    _noMoreData = tmp.count < 10;
    if (!_noMoreData) {
        [_tableView.mj_footer endRefreshing];
    }else{
        [_tableView.mj_footer endRefreshingWithNoMoreData];
    }
    _page ++;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)willResignActive{
    [super willResignActive];
    
}

- (void)didBecomeActive{
    [super didBecomeActive];
}


#pragma mark - tableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //return arc4random()%100;
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 94;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

@end
